﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace cs499.Preferences
{
    public partial class ListPreferences : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["action"] != null)
            {
                if (Request.QueryString["action"] != "")
                {
                    ListActionMessage.Text = Request.QueryString["action"] + "<br />";
                }
            }
            else
            {
                ListActionMessage.Text = "";
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            LinkButton preferencesMenuButton = (LinkButton)Master.FindControl("LoginView").FindControl("PreferencesMenuButton");
            preferencesMenuButton.CssClass = "selected";
        }

        protected void UserDropDownList_DataBound(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.IsInRole("admin"))
            {
                try
                {
                    if (UserDropDownList.Items.FindByText(HttpContext.Current.User.Identity.Name) == null)
                    {
                        ObjectDataSource1.SelectParameters["UserName"].DefaultValue = HttpContext.Current.User.Identity.Name;
                    }
                    else
                    {
                        UserDropDownList.SelectedValue = UserDropDownList.Items.FindByText(HttpContext.Current.User.Identity.Name).Value;
                    }
                }
                catch (Exception)
                {

                }
                UserDropDownList.Enabled = false;
                UserDropDownList.Visible = false;
            }
            else
            {
                ObjectDataSource1.SelectParameters["UserName"].DefaultValue = UserDropDownList.SelectedValue;
            }

            
            GridView1.DataBind();
        }

        protected void GridView1_DataBinding(object sender, EventArgs e)
        {
            
        }

        protected void GridView1_CallingDataMethods(object sender, CallingDataMethodsEventArgs e)
        {
            ObjectDataSource1.SelectParameters["UserName"].DefaultValue = UserDropDownList.SelectedValue;
        }

        protected void UserDropDownList_TextChanged(object sender, EventArgs e)
        {
            ObjectDataSource1.SelectParameters["UserName"].DefaultValue = UserDropDownList.SelectedValue;
            GridView1.DataBind();
        }
    }
}